package com.groupfour.attendancemanagementsystem.controller;

import com.groupfour.attendancemanagementsystem.entity.DTO.CRUClassesDTO;
import com.groupfour.attendancemanagementsystem.entity.VO.RClassesVO;
import com.groupfour.attendancemanagementsystem.entity.VO.RDepartmentVO;
import com.groupfour.attendancemanagementsystem.entity.VO.RMajorVO;
import com.groupfour.attendancemanagementsystem.entity.VO.RTeacherVO;
import com.groupfour.attendancemanagementsystem.service.ClassesService;
import com.groupfour.attendancemanagementsystem.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/classesManagement")
public class ClassesManagementController {

    private final Integer pageSize = 10;

    private ClassesService classesService;

    @Autowired
    public ClassesManagementController(ClassesService classesService) {
        this.classesService = classesService;
    }


    /**
     * @author 卑微小峰
     * @describe 查询所有院系
     */
    @GetMapping("/selectAllDepartment")
    public Result selectAllDepartment() {
        List<RDepartmentVO> departmentList = classesService.selectAllDepartment();
        if (departmentList != null) {
            return Result.ok(departmentList);
        }
        return Result.fail().message("暂无数据");
    }

    /**
     * @author 卑微小峰
     * @param departmentCode
     * @describe 查询所有专业（全校或某一院系下）
     */
    @GetMapping("/selectAllMajor/{departmentCode}")
    public Result selectAllMajor(@PathVariable("departmentCode") String departmentCode) {

        System.out.println("xxxx:"+departmentCode);
        List<RMajorVO> majorList = classesService.selectAllMajor(departmentCode);
        if (majorList != null) {
            return Result.ok(majorList);
        }
        return Result.fail().message("暂无数据");
    }

    /**
     * @author 卑微小峰
     * @describe 查询所有在职教师姓名
     */
    @GetMapping("/selectAllTeacherNameByEmploymentStatus")
    public Result selectAllTeacherNameByEmploymentStatus() {
        List<RTeacherVO> teacherNameList = classesService.selectAllTeacherNameByEmploymentStatus();
        if (teacherNameList != null) {
            return Result.ok(teacherNameList);
        }
        return Result.fail().message("暂无教师数据");
    }

    /**
     * @author 扶永超
     * @param cruClassesDTO
     * @describe 添加班级信息
     */
    @PostMapping("/doAddClasses")
    public Result<String> doAddClasses(@RequestParam CRUClassesDTO cruClassesDTO){
        int result = classesService.addClasses(cruClassesDTO);
        if(result>0){
           return Result.ok("添加");
        }else{
           return Result.fail("添加");
        }
    }

    /**
     * @author 扶永超
     * @param classesCode
     * @param cruClassesDTO
     * @describe 修改班级信息（根据班级编号classesCode）
     */
    @PostMapping("/doUpdateClasses/{classesCode}")
    public Result<String> doUpdateClasses(@PathVariable("classesCode")String classesCode, CRUClassesDTO cruClassesDTO){
        int result = classesService.updateClasses(cruClassesDTO, classesCode);
        if(result>0){
            return Result.ok("修改");
        }else{
            return Result.fail("修改");
        }
    }


    /**
     * @author 扶永超
     * @param cruClassesDTO
     * @describe 查询班级信息
     */
    @PostMapping("/doSelectClasses/{pageNum}/{pageSize}")
    public Result doSelectClasses(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            @RequestBody CRUClassesDTO cruClassesDTO){
        Map<String,Object> classesMap = classesService.selectClasses(cruClassesDTO,pageNum,pageSize);
        if((Long)classesMap.get("pageCount") > 0){
            return Result.ok(classesMap);
        }else{
            return Result.fail();
        }
    }


    /**
     * @author 卑微小峰
     * @param classesCode
     * @describe 查询指定班级信息（用于修改页面显示数据）
     */
    @GetMapping("/doSelectClassesByClassesCode")
    public Result doSelectClassesByClassesCode(@RequestParam String classesCode) {
        RClassesVO classes = classesService.selectClassesByClassesCode(classesCode);
        if (classes != null) {
            return Result.ok(classes);
        }
        return Result.fail().message("查询出错");
    }
}
